package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

public class ChangePassword {

	private String currentUser;
	private String oldPassword;
	private String newPassword;
	private String reEnterNewpassword;
	private String hiddenUserName;

	
	public String changeUserPassword(){
		System.out.println("currentUser:"+hiddenUserName);
		System.out.println("oldPassword:"+oldPassword);
		System.out.println("newPassword:"+newPassword);
		System.out.println("reEnterNewpassword:"+reEnterNewpassword);
		
		 try{
             Properties props = new Properties();
             props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
             System.out.println("props.getProperty(username):"+props.getProperty(hiddenUserName));
             
             if((oldPassword==null || oldPassword.trim().equals(""))){
                 FacesMessage message = new FacesMessage("Old Password should not be null");
                 FacesContext.getCurrentInstance().addMessage("Old Password should not be null", message);
                 
                 return "changePassword";
             }else if((newPassword==null || newPassword.trim().equals(""))){
                 FacesMessage message = new FacesMessage("New Password should not be null");
                 FacesContext.getCurrentInstance().addMessage("New Password should not be null", message);
                 
                 return "changePassword";
             }else if((reEnterNewpassword==null || reEnterNewpassword.trim().equals(""))){
                 FacesMessage message = new FacesMessage("ReEnter Password should not be null");
                 FacesContext.getCurrentInstance().addMessage("ReEnter Password should not be null", message);
                 
                 return "changePassword";
             }else if(!newPassword.equals(reEnterNewpassword)){
                 FacesMessage message = new FacesMessage("NewPassword and ReEnter Password should be same");
                 FacesContext.getCurrentInstance().addMessage("NewPassword and ReEnter Password should be same", message);
                 
                 return "changePassword";
             }
             if(props.getProperty(hiddenUserName)!=null && !props.getProperty(hiddenUserName).equals(oldPassword)){
                     System.out.println("with in password check");
                     FacesMessage message = new FacesMessage("Old Password incorrect for User:"+hiddenUserName);
                     FacesContext.getCurrentInstance().addMessage("Old Password incorrect for User:"+hiddenUserName, message);
                     return "changePassword";
             }else if(props.getProperty(hiddenUserName)!=null && props.getProperty(hiddenUserName).equals(newPassword)){
                 System.out.println("with in password check");
                 FacesMessage message = new FacesMessage("Old Password New Password are same pls give different password");
                 FacesContext.getCurrentInstance().addMessage("Old Password New Password are same pls give different password", message);
                 return "changePassword";
             }else{
            	 props.put(hiddenUserName, newPassword);
            	 props.store(new FileOutputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"),"Saving UserDetails");
            	 return "passwordChangeSuccess";
             }
             
             
     }catch(Exception e){
             e.printStackTrace();
     }
		
		return null;
	}
	public String goToLoginPage(){
		return "loginPage";
	}
	public String goToJavaIDE(){
		return "gotoJavaIDE";
	}
	public String getHiddenUserName() {
		return hiddenUserName;
	}
	public void setHiddenUserName(String hiddenUserName) {
		this.hiddenUserName = hiddenUserName;
	}
	public String getCurrentUser() {
		return currentUser;
	}
	public void setCurrentUser(String currentUser) {
		this.currentUser = currentUser;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getReEnterNewpassword() {
		return reEnterNewpassword;
	}
	public void setReEnterNewpassword(String reEnterNewpassword) {
		this.reEnterNewpassword = reEnterNewpassword;
	}
	
}
